using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component.Operation
{
	public class Game5Component : OperationComponent
	{
		private const float MIN_1_A_X = -1.33f;

		private const float MAX_1_A_X = 1.37f;

		private const float MIN_1_B_X = -1.8f;

		private const float MAX_1_B_X = 1.8f;

		private const float MIN_2_A_X = -1.42f;

		private const float MAX_2_A_X = 1.24f;

		private const float MIN_2_B_X = -0.9f;

		private const float MAX_2_B_X = 3.3f;

		private const float MIN_X = -3.5f;

		private const float MAX_X = 3.5f;

		private const float UP_Y = 1.3f;

		private const float MIDDLE_Y = 0f;

		private const float DOWN_Y = -1.3f;

		private Transform btnBack;

		private Transform tile1A;

		private Transform tile1B;

		private Transform tile2A;

		private Transform tile2B;

		private Transform selectTile;

		private Transform targetTile;

		private bool tile1Complete;

		private bool tile2Complete;

		private List<Transform> efcs = new List<Transform>();

		private Vector2 lastTouchPoint;

		private float minX;

		private float maxX;

		private float newX;

		private float newY;

		private void Awake()
		{
			btnBack = base.transform.Find("BtnBack");
			tile1A = base.transform.Find("Tile1A");
			tile1A.SetLocalX(Random.Range(-1.33f, 1.37f));
			tile1B = base.transform.Find("Tile1B");
			tile1B.SetLocalX(Random.Range(-1.8f, 1.8f));
			tile2A = base.transform.Find("Tile2A");
			tile2A.SetLocalX(Random.Range(-1.42f, 1.24f));
			tile2B = base.transform.Find("Tile2B");
			tile2B.SetLocalX(Random.Range(-0.9f, 3.3f));
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (displayer == null)
			{
				return;
			}
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 1);
			selectTile = null;
			targetTile = null;
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btnBack)
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
					displayer.ChangeType(OperationDisplayerItem.Type.Main);
				}
				else if (raycastHit2D.collider.transform == tile1A)
				{
					if (!tile1Complete)
					{
						selectTile = tile1A;
						targetTile = tile1B;
						minX = -1.33f;
						maxX = 1.37f;
					}
				}
				else if (raycastHit2D.collider.transform == tile1B)
				{
					if (!tile1Complete)
					{
						selectTile = tile1B;
						targetTile = tile1A;
						minX = -1.8f;
						maxX = 1.8f;
					}
				}
				else if (raycastHit2D.collider.transform == tile2A)
				{
					if (!tile2Complete)
					{
						selectTile = tile2A;
						targetTile = tile2B;
						minX = -1.42f;
						maxX = 1.24f;
					}
				}
				else if (raycastHit2D.collider.transform == tile2B && !tile2Complete)
				{
					selectTile = tile2B;
					targetTile = tile2A;
					minX = -0.9f;
					maxX = 3.3f;
				}
			}
			lastTouchPoint = touchEventData.curPos;
		}

		public override void TouchMove(TouchEventData touchEventData)
		{
			base.TouchMove(touchEventData);
			if (selectTile != null)
			{
				newX = selectTile.localPosition.x + touchEventData.curPos.x - lastTouchPoint.x;
				if (newX < minX)
				{
					newX = minX;
				}
				else if (newX > maxX)
				{
					newX = maxX;
				}
				if (Mathf.Abs(newX - targetTile.localPosition.x) < 0.1f)
				{
					newX = targetTile.localPosition.x;
					selectTile.SetLocalX(newX);
					Singleton<AudioManager>.Instance.PlaySound("sound_embed");
					Singleton<AudioManager>.Instance.PlaySound("sound_blink");
					Transform transform = null;
					if (selectTile == tile1A || selectTile == tile1B)
					{
						tile1Complete = true;
						transform = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", tile1B.transform.position + new Vector3(0.2f, 0.8f, -1E-05f), 3f);
						tile1A.GetComponent<BoxCollider2D>().enabled = false;
						tile1B.GetComponent<BoxCollider2D>().enabled = false;
					}
					else if (selectTile == tile2A || selectTile == tile2B)
					{
						tile2Complete = true;
						transform = Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", tile2B.transform.position + new Vector3(-1f, 2f, -1E-05f), 3f);
						tile2A.GetComponent<BoxCollider2D>().enabled = false;
						tile2B.GetComponent<BoxCollider2D>().enabled = false;
					}
					transform.SetParent(base.transform);
					efcs.Add(transform);
					selectTile = null;
					targetTile = null;
				}
				else
				{
					selectTile.SetLocalX(newX);
				}
			}
			lastTouchPoint = touchEventData.curPos;
		}

		private void OnDestroy()
		{
			for (int i = 0; i < efcs.Count; i++)
			{
				if (efcs[i] != null)
				{
					Singleton<GamePoolManager>.Instance.Despawn(efcs[i]);
				}
			}
			efcs.Clear();
		}
	}
}
